package com.qxglxt.qxglxt.controller;
import javax.annotation.Resource;

import com.qxglxt.qxglxt.dao.IUsersDao;
import com.qxglxt.qxglxt.dto.UsersDTO;
import com.qxglxt.qxglxt.model.Users;
import com.qxglxt.qxglxt.service.IUsersService;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Resource
    private IUsersDao usersDao;
    @Resource
    private IUsersService usersService;
    @ApiOperation(value = "查询所有用户",notes = "查询所有用户")
    @GetMapping
    public Map<String ,Object> findAll(){
        List<Users> list=usersDao.findAll();
        Map<String,Object> map=new HashMap<String ,Object>();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", usersDao.count());
        map.put("data", list);
        return map;
    }
     @ApiOperation(value = "多条件分页查询用户",notes = "多条件分页查询用户")
    @GetMapping(value = "/findPage")
    public Page<Users> findPage(@RequestBody UsersDTO dto){
        return usersService.findAllPage(dto);
    } 
    /* @ApiOperation(value = "多条件分页查询用户",notes = "多条件分页查询用户")
    @GetMapping(value = "/findPage")
    public Map<String ,Object> findPage(@RequestBody UsersDTO dto){
        Page<Users> page=usersService.findAllPage(dto);
        Map<String ,Object> map=new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", page.getNumberOfElements());
        map.put("data",page.getContent());
        return map;
    }  */
    @ApiOperation(value = "添加用户信息",notes = "添加用户信息")
    @PostMapping(value = "/addUser")
    public Users addUsers(@RequestBody Users u){
        return usersService.saveUsers(u);
    }
    @ApiOperation(value = "更新用户信息",notes = "更新用户信息")
    @PutMapping(value = "/updateUser")
    public Users editUser(@RequestBody Users users){
        return usersService.saveUsers(users);
    }
    @ApiOperation(value = "删除用户信息",notes = "删除用户信息")
    @ApiImplicitParam(name = "id",value = "编号",required = true,paramType = "path")
    @DeleteMapping(value = "/{id}")
    public void delUserById(@PathVariable Integer id){
        usersService.delUsersById(id);
    } 
    @ApiOperation(value = "重置密码",notes = "重置密码")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
    @PutMapping(value = "/{id}")
    public int pwdReset(@PathVariable Integer id){
        return usersDao.pwdReset(id);
    }
    @ApiOperation(value = "锁定用户",notes = "锁定用户")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
    @PutMapping("/lockoutUser/{id}")
    public int lockoutUser(@PathVariable Integer id){
        return usersDao.lockoutUser(id);
    }
    @ApiOperation(value = "解锁用户",notes = "解锁用户")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
    @PutMapping("/notLockoutUser/{id}")
    public int notLockoutUser(@PathVariable Integer id){
        return usersDao.notLockoutUser(id);
    }
    /* @ApiOperation(value = "查询用户角色",notes = "查询用户角色")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
    @GetMapping("/findRoleById/{id}")
    public Users findUserRole(@PathVariable Integer id){
        return usersDao.findRoleById(id);
    } */

    @ApiOperation(value = "给用户设置角色",notes = "给用户设置角色" )
    @PostMapping("/setUserRole")
    public void setUserRole(String  userId,@RequestParam List<String> roleIdList ){
        usersService.setUserRole(userId, roleIdList);
    }

    @ApiOperation(value = "移除用户角色",notes = "移除用户角色" )
    @PostMapping("/betachUserRoles/")
    public int betachUserRoles(String userId,@RequestParam List<String> rolesId){
        return usersDao.betachUserRoles(userId, rolesId);
    }

    @ApiOperation(value = "用户登录功能",notes = "用户登录功能" )
    @ApiImplicitParams({
        @ApiImplicitParam(name = "loginName",value = "用户登录名",required = true,paramType = "path" ),
        @ApiImplicitParam(name = "passWord",value = "用户登录密码",required = true,paramType = "path" )
    })
    @GetMapping("/login/{loginName}/{passWord}")
    public Users loginUsers(@PathVariable String loginName,@PathVariable String passWord){
        return usersDao.login(loginName, passWord);
    }

}